package Data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class SaleTacticsDataOperator implements IDataReader,IDataUpdate,IDataWriter{
	ArrayList<SaleTacticsData>  SaleTacticsList;
	String filename = "D:UserSaleTacticsData.txt";
	
	public SaleTacticsDataOperator(){
		SaleTacticsList = new ArrayList<SaleTacticsData>();
		File file = new File(filename);
		try{
			SaleTacticsData sd = new SaleTacticsData();
			BufferedReader bf = new BufferedReader(new FileReader(file));
			String[] record = null;
			String data = "";
			while((data = bf.readLine()) != null){
				if(data.trim().equals("")){
					continue;
				}
				record = splitText(data);
				sd = new SaleTacticsData(Integer.parseInt(record[0]),Double.parseDouble(record[1]),record[2],record[3]);
			}
			}catch(IOException ex){
				ex.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public String[] splitText(String s) {
		String[] data = s.split(",");
		return data;
	}

	@Override
	public Data readDataByID(int id) {
		for(SaleTacticsData sd:SaleTacticsList){
			if(sd.getID() == id){
				return sd;
			}
		}
		return null;
	}

	@Override
	public void startUpdate() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void saveData() {
		// TODO Auto-generated method stub
		
	}
	
	public void readFile(String fileName){
		try{
			String info = "";
			File file = new File(fileName);
			BufferedReader bf= new BufferedReader(new FileReader(file));
			while((info = bf.readLine()) != null){
				System.out.println(info);
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
}
